package org.yanfengying.mkt.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.yanfengying.dto.ClueBusinessDTO;
import org.yanfengying.mkt.domain.Business;
import org.yanfengying.mkt.domain.BusinessProduct;
import org.yanfengying.mkt.domain.Clue;
import org.yanfengying.mkt.domain.ClueRemark;
import org.yanfengying.mkt.mapper.BusinessProductMapper;
import org.yanfengying.mkt.mapper.ClueActivityMapper;
import org.yanfengying.mkt.mapper.ClueMapper;
import org.yanfengying.mkt.mapper.ClueRemarkMapper;
import org.yanfengying.mkt.service.IClueService;
import org.yanfengying.org.domain.Employee;
import org.yanfengying.org.mapper.EmployeeMapper;
import org.yanfengying.prod.domain.Product;
import org.yanfengying.prod.mapper.ProductMapper;
import org.yanfengying.utils.LoginContext;
import org.yanfengying.utils.service.impl.BastServerImpl;
import org.springframework.stereotype.Service;
import org.yanfengying.vo.ClueResuorceVO;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanfengying
 * @since 2022-10-24
 */
@Service
public class ClueServiceImpl extends BastServerImpl<Clue> implements IClueService {


    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ClueRemarkMapper clueRemarkMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private BusinessProductMapper businessProductMapper;
    @Override
    @Transactional
    public void clueAssignSave(Clue clue) {
        //更改跟进人
        //修改线索状态
        clue.setState(1);
        clue.setEditTime(new Date());
        clue.setEditBy(LoginContext.getLoginUser().getId()+"");
        //更改信息
        clueMapper.update(clue);

        //记录操作时间,操作人员
        ClueRemark clueRemark = new ClueRemark();
        //设置更改者id
        clueRemark.setClueId(clue.getId());
        //设置谁修改了什么
        clueRemark.setNoteContent(clue.getFullName()+"被"+employeeMapper.loadById(clue.getOwner()).getUsername()+"跟进了");
        //设置修改时间
        clueRemark.setEditTime(new Date());
        //设置被谁修改
        clueRemark.setEditBy(LoginContext.getLoginUser().getId());
        //保存记录
        clueRemarkMapper.insert(clueRemark);
    }

    @Override
    @Transactional
    public void clueBusniessSave(ClueBusinessDTO dto) {
        Clue clue = dto.getClue();
        Long productId = dto.getProduct().getId();
        //获取当前产品的所有信息
        Product product = productMapper.loadById(productId);
        String username = LoginContext.getLoginUser().getUsername();
        //修改线索的状态为 2-已转化为商机  设置线索备注 修改人修改时间
        clue.setEditBy(username);
        clue.setEditTime(new Date());
        clue.setState(2);
        clueMapper.update(clue);
        //备注
        ClueRemark clueRemark = new ClueRemark();
        clueRemark.setEditBy(LoginContext.getLoginUser().getId());
        clueRemark.setNoteContent(clue.getFullName()+"已被转化为商机,操作人:"+username);
        clueRemark.setClueId(clue.getId());
        clueRemark.setCreateTime(new Date());
        clueRemarkMapper.insert(clueRemark);
        //创建商机
        Business business = new Business();
        business.setName("客户"+clue.getFullName()+"对"+product.getName()+"产品有购买意向");
        business.setClueId(clue.getId());
        business.setProductId(productId);
        business.setProductName(product.getName());
        business.setProductPrice(product.getPrice());
        business.setState(0);
        //将商机和产品相关信息放到关联表中
        BusinessProduct businessProduct = new BusinessProduct();
        BeanUtils.copyProperties(product, businessProduct);
        businessProduct.setId(null);  //因为要新增所以不需要id
        businessProduct.setBusinessId(business.getId());
        businessProductMapper.insert(businessProduct);

    }

    /**
     * 图表展示数据获取
     * @return
     */
    @Override
    public List<ClueResuorceVO> loadResource() {
        return clueMapper.loadResource();
    }
}
